package com.lankystar.logistucs.spring.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lankystar.logistucs.spring.dao.IBrokerDao;
import com.lankystar.logistucs.spring.dao.IOneOrderAndOneStatusDao;
import com.lankystar.logistucs.spring.dao.IOneOrderDao;
import com.lankystar.logistucs.spring.dao.IOrderStatusDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderAndTwoStatusDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderDao;
import com.lankystar.logistucs.spring.entity.Cargo;
import com.lankystar.logistucs.spring.entity.OneOrder;
import com.lankystar.logistucs.spring.entity.OneOrderAndOneStatus;
import com.lankystar.logistucs.spring.entity.OneStatus;
import com.lankystar.logistucs.spring.entity.Truck;
import com.lankystar.logistucs.spring.entity.TwoOrder;
import com.lankystar.logistucs.spring.entity.TwoOrderAndTwoStatus;
import com.lankystar.logistucs.spring.entity.TwoStatus;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.service.BrokerService;

@Service
public class BrokerServiceImpl implements BrokerService{

	@Autowired
	private IBrokerDao brokerDao;
	@Autowired
	private ITwoOrderDao twoOrderDao;
	@Autowired
	private IOneOrderDao oneOrderDao;
	@Autowired
	private IOrderStatusDao orderStatusDao;
	@Autowired
	private ITwoOrderAndTwoStatusDao tatDao;
	@Autowired
	private IOneOrderAndOneStatusDao oaoDao;
	@Override
	public List<Map<String, Object>> queryCargoAndTwoOrder(int brokerId) {
		List<Map<String, Object>>  maplist = new ArrayList<>();
		List<Object[]> oblist = brokerDao.selectCargoAndTwoOrder(brokerId);
		for (Object[] obs : oblist) {
			if(obs[1]==null){
				Map<String, Object> map = new HashMap<>();
				TwoOrder twoOrder = (TwoOrder) obs[0];
				Truck truck = twoOrder.getTruck();//车辆
				User user = twoOrder.getUser();//司机
				Cargo cargo = (Cargo) obs[3];
				map.put("twoOrderId", twoOrder.getTwoOrderId());
				map.put("totalOrderNu", cargo.getTotalOrderNu());
				map.put("pointOrderNu", cargo.getPointOrderNu());
				map.put("cardNu", truck.getCardNu());//白卡号
				map.put("uploadPdf", cargo.getUploadPdf());
				map.put("userName", user.getUserName());
				maplist.add(map);
			}
		}
		return maplist;
	}
	@Override
	public String updateTwoOrder(int twoOrderId, int twoStatusKey) {
		if(twoStatusKey==0){
			TwoOrder twoOrder = twoOrderDao.selectTwoOrderById(twoOrderId);
			TwoStatus twoStatus = orderStatusDao.selectTwoStatusBykey(1);
			TwoOrderAndTwoStatus tat = new TwoOrderAndTwoStatus();
			tat.setTwoOrder(twoOrder);
			tat.setTwoStatus(twoStatus);
			tat.setCreateTime(new Date().getTime());
			tatDao.add(tat);
			return String.valueOf(tat.getCreateTime());
		}else{
			TwoOrder twoOrder = twoOrderDao.selectTwoOrderById(twoOrderId);
			TwoStatus twoStatus = orderStatusDao.selectTwoStatusBykey(twoStatusKey);
			TwoOrderAndTwoStatus tat = new TwoOrderAndTwoStatus();
			tat.setTwoOrder(twoOrder);
			tat.setTwoStatus(twoStatus);
			tat.setCreateTime(new Date().getTime());
			tatDao.add(tat);
			/**
			 * 当上海报关结束，生成一级订单状态
			 */
			if(twoStatusKey==30){
				OneOrderAndOneStatus oao = new OneOrderAndOneStatus();
				OneOrder oneOrder = oneOrderDao.selectOneOrderByTwoOrderId(twoOrderId);
				OneStatus oneStatus = orderStatusDao.selectOneStatusBykey(twoStatusKey);
				oneOrder.setType(1);
				twoOrder.setType(1);
				oneOrderDao.update(oneOrder);
				twoOrderDao.update(twoOrder);
				oao.setOneOrder(oneOrder);
				oao.setOneStatus(oneStatus);
				oao.setCreateTime(new Date().getTime());
				oaoDao.add(oao);
			}
			return String.valueOf(tat.getCreateTime());
		}
		
	}
	@Override
	public List<Map<String, Object>> queryRecelveOrder(int brokerId,String createTime) {
		List<Map<String, Object>>  maplist = new ArrayList<>();
		List<Object[]> oblist = brokerDao.selectRecelveOrder(brokerId,createTime);
		for (Object[] obs : oblist) {
			Map<String, Object> map = new HashMap<>();
			TwoOrderAndTwoStatus tat = (TwoOrderAndTwoStatus) obs[1];
			TwoStatus twoStatus = tat.getTwoStatus();
			TwoOrder twoOrder = (TwoOrder) obs[0];
			Truck truck = twoOrder.getTruck();//车辆
			//User user = twoOrder.getUser();//司机
			Cargo cargo = (Cargo) obs[3];
			map.put("twoOrderId", twoOrder.getTwoOrderId());
			map.put("totalOrderNu", cargo.getTotalOrderNu());
			map.put("pointOrderNu", cargo.getPointOrderNu());
			map.put("cardNu", truck.getCardNu());//白卡号
			map.put("uploadPdf", cargo.getUploadPdf());
			map.put("twoStatusKey", twoStatus.getTwoStatusKey());
			map.put("twoStatusValue", twoStatus.getTwoStatusValue());
			maplist.add(map);
		}
		return maplist;
	}
	@Override
	public List<Map<String, Object>> queryOrderlist(int companyId) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		List<Map<String, Object>> maplist = new ArrayList<>();
		List<Object[]> oblist = brokerDao.selectOrderlist(companyId);
		for (Object[] obs : oblist) {
			Map<String, Object> map = new HashMap<>();
			TwoOrder twoOrder = (TwoOrder) obs[0];
			Cargo cargo = (Cargo) obs[2];
			map.put("twoOrderId", twoOrder.getTwoOrderId());
			map.put("totalOrderNu", cargo.getTotalOrderNu());
			map.put("pointOrderNu", cargo.getPointOrderNu());
			map.put("cargoId", cargo.getCargoId());
			map.put("createTime", sdf.format(new Date(cargo.getCreateTime())));
			maplist.add(map);
		}
		return maplist;
	}

}
